package com.calmcar.adas.gps;

import android.content.Context;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.util.Log;

import com.calmcar.adas.apiserver.AdasConf;
import com.calmcar.adas.conf.Constant;

import java.util.Iterator;

import static android.content.ContentValues.TAG;

/**
 * Created by lelexiao on 2018/4/18.
 */

public class AdasLocManager {
    public LocationTickListener getLocationTickListener() {
        return locationTickListener;
    }

    public void setLocationTickListener(LocationTickListener locationTickListener) {
        this.locationTickListener = locationTickListener;
    }

    public  AdasLocManager(Context mContext) {
        this.mContext = mContext;
    }

    private Context mContext;
    private LocationTickListener locationTickListener;
    public LocationManager locationManager;

    private  double longitude ;
    private  double latitude;
    private  double oldSpeed;
    private  int   gpsStateLevel;

    public double getRate() {
        return rate;
    }

    public void setRate(double rate) {
        this.rate = rate;
    }

    private  double rate;
    private  long lastUpdateTime;

    public int getGpsAvailableLevel() {
        return gpsAvailableLevel;
    }

    public void setGpsAvailableLevel(int gpsAvailableLevel) {
        this.gpsAvailableLevel = gpsAvailableLevel;
    }

    private  int gpsAvailableLevel = -1;//-1：没有该模块 0：
//    private  Handler handlerGps ;

//    public HandlerThread handlerThreadGps ;

    public void locationProcessServiceInitStart(){
        initLocation();
        if(AdasConf.RUN_MODE==0){
            this.rate=50.99999;
        }else{
            this.rate=3.6;
        }
    }


    public void  locationProcessServiceStop(){
        if (mLocationManager != null) {
            mLocationManager.removeUpdates(mLocationListener);
            mLocationManager.removeGpsStatusListener(mGpsStatusListener);
        }
    }


    double oldLongitude ;
    double oldLatitude ;
    public boolean checkIsLocationSame(double longitude,double latitude){
        if(oldLatitude== latitude  && oldLongitude==longitude ){
            return true;
        }
        oldLongitude=longitude;
        oldLatitude=latitude;
        return false;
    }




    public boolean isValidLocationState(){
        return  getGpsAvailableLevel()> Constant.GPS_LEVEL;
    }



    //新方法--------------------sta
    LocationManager mLocationManager;
    LocationListener mLocationListener;
    GpsStatus.Listener mGpsStatusListener ;
    Iterator<GpsSatellite>  mIterator;
    private void initLocation() {

        mLocationManager = (LocationManager) mContext. getSystemService(Context.LOCATION_SERVICE);
        mLocationListener = new LocationListener() {

            public void onStatusChanged(String provider, int status, Bundle extras) {
                if (status == LocationProvider.OUT_OF_SERVICE || status == LocationProvider.TEMPORARILY_UNAVAILABLE) {
                    gpsAvailableLevel=0;
                    rate=3.6;
                    if(AdasConf.RUN_MODE==0){
                       rate=50.99999;
                    }
                }
            }

            public void onProviderEnabled(String provider) {

            }

            public void onProviderDisabled(String provider) {

            }

            public void onLocationChanged(Location location) {
                updateLocation(location);
                if(locationTickListener !=null ){
                    locationTickListener.onTickArrive(longitude,latitude ,rate  );
                }
            }
        };

        mGpsStatusListener = new GpsStatus.Listener() {
            @Override
            public void onGpsStatusChanged(int event) throws  SecurityException {
                if(event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
                    GpsStatus xGpsStatus = mLocationManager.getGpsStatus(null);
                    Iterable<GpsSatellite> iStatellite = xGpsStatus.getSatellites();
                    mIterator = iStatellite.iterator();

                    while (mIterator.hasNext()) {
                        GpsSatellite satellite = mIterator.next();

                        float snr = satellite.getSnr();
                        int   prn = satellite.getPrn();
                        float azi = satellite.getAzimuth();
                        float ele = satellite.getElevation();

                    }
                }
            }
        };

        startGpsTest();
    }

    private void startGpsTest()  throws  SecurityException {
        if (mLocationManager != null) {
            if (mLocationListener != null) {
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListener);
            }
            if (mGpsStatusListener != null) {
                mLocationManager.addGpsStatusListener(mGpsStatusListener);
            }
        }
    }

    protected void updateLocation(Location location) {
        if (location != null){
            gpsAvailableLevel=1;
            lastUpdateTime = System.currentTimeMillis();
            longitude = location.getLongitude();
            latitude  = location.getLatitude();
            double speed= location.getSpeed();
            if(checkIsLocationSame(longitude, latitude) && oldSpeed == speed && speed>0.5){
                speed=1;
            }
            oldSpeed=speed;
            double r = speed *3600/1000;
            this.rate = r;
        }else {
            gpsAvailableLevel=0;
            this.rate=3.6;
        }
        if(AdasConf.RUN_MODE==0){
            this.rate=50.99999;
        }
    }
    //新方法--------------------end
}
